﻿//  Copyright (c) 2011 Ray Liang (http://www.dotnetage.com)
//  Licensed MIT: http://www.opensource.org/licenses/mit-license.php

using System;
using System.Data.Entity;
using System.Collections.Generic;

namespace DNA.Mvc.Data.Entity
{
    public abstract class UnitOfWorkBase<TDBContext> : IUnitOfWork
        where TDBContext :DbContext
    {
        protected TDBContext dbContext;

        private IDictionary<Type,object> repositoryTable = new Dictionary<Type,object>();
        
        private IRepository<T> GetRepository<T>()
            where T:class
        {
            IRepository<T> repository = null;
            var key=typeof(T);
            
            if (repositoryTable.ContainsKey(key))
                repository = (IRepository<T>)repositoryTable[key];
            else
            {
                repository = GenericRepository<T>();
                repositoryTable.Add(key, repository);
            }

            return repository;
        }

        protected virtual IRepository<T> GenericRepository<T>() where T : class
        {
            return new GenericEntityRepostory<T>(dbContext);
        }

        public T Find<T>(object id) where T : class
        {
            return GetRepository<T>().Find(id);
        }

        public T Add<T>(T t) where T : class
        {
            return GetRepository<T>().Create(t);
        }

        public void Update<T>(T t) where T : class
        {
            GetRepository<T>().Update(t);
        }

        public void Delete<T>(T t) where T : class
        {
            GetRepository<T>().Delete(t);
        }

        public int SaveChanges(bool validateOnSave = true)
        {
            if (!validateOnSave)
                dbContext.Configuration.ValidateOnSaveEnabled = false;

            return dbContext.SaveChanges();
        }

        public void Dispose()
        {
            if (dbContext != null)
                dbContext.Dispose();
            GC.SuppressFinalize(this);
        }

        
        public System.Linq.IQueryable<T> Where<T>(System.Linq.Expressions.Expression<Func<T, bool>> predicate)
            where T:class
        {
            return GetRepository<T>().Filter(predicate);
        }


        public T Find<T>(System.Linq.Expressions.Expression<Func<T, bool>> predicate) where T : class
        {
            return GetRepository<T>().Find(predicate);
        }


        public System.Linq.IQueryable<T> All<T>() where T : class
        {
            return GetRepository<T>().All();
        }


        public int Count<T>() where T : class
        {
            return GetRepository<T>().Count();
        }

        public int Count<T>(System.Linq.Expressions.Expression<Func<T, bool>> predicate) where T : class
        {
            return GetRepository<T>().Count(predicate);
        }
    }

    public class GenericUnitOfWork<TDbContext> : UnitOfWorkBase<TDbContext>
        where TDbContext:DbContext
    {
        public GenericUnitOfWork()
        {
            this.dbContext = Activator.CreateInstance<TDbContext>();
        }

        public GenericUnitOfWork(TDbContext context) { this.dbContext = context; }
    }
}
